<template>
	<el-container>
		<el-header>
			<el-button type="primary" @click="add_bt">添加</el-button>
		</el-header>
		<el-main>
			<el-table :data="list" style="width: 100%">
				<el-table-column label="ID" prop="id">
				</el-table-column>
				<el-table-column label="名称" prop="name">
				</el-table-column>
				<el-table-column label="楼层ID" prop="pid">
				</el-table-column>
				<el-table-column align="right">
					<template slot-scope="scope">
						<el-button size="mini" @click="edit_bt(scope.row)">编辑</el-button>
						<el-button size="mini" type="danger" @click="del(scope.row.id)">删除
						</el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-dialog :title="edit_type==0?'添加':'编辑'" :visible.sync="visible" width="40%" :before-close="handleClose">
				<el-form ref="form" :model="form" label-width="80px">
					<el-form-item label="名称">
						<el-input v-model="form.name"></el-input>
					</el-form-item>
					<el-form-item label="楼层">
						<el-select v-model="form.pid" filterable laceholder="请选择">
							<el-option v-for="(item,index) in properties" :key="index" :label="item.name"
								:value="item.id">
							</el-option>
						</el-select>
						<el-form-item></el-form-item>
						<el-button type="primary" @click="submit">提交</el-button>
						<el-button type="primary" @click="clear">取消</el-button>
					</el-form-item>
				</el-form>
			</el-dialog>
		</el-main>
		<el-footer>
			<el-pagination background layout="prev, pager, next" :page-size="search_form.num" :total="total"
				@current-change="current_change">
			</el-pagination>
		</el-footer>
	</el-container>
</template>
<script>
	export default {
		data() {
			return {
				list: [],
				form: {
					name: "",
					table: "floors",
					pid: ""
				},
				properties: [],
				total: 0,
				url: ["/cms/table/insert", "/cms/table/update"],
				edit_tips: ["添加成功", "修改成功"],
				edit_type: 0, //0添加1编辑
				visible: false,
				search_form: {
					table: "floors",
					page: 1,
					num: 5,
					where: null,
					orderBy: "id",
					asdes: "asc"
				}
			}
		},
		mounted() {
			this.get_data()
			this.get_properties()
		},
		methods: {
			get_properties() {
				let data = {
					table: "properties",
					select: ["id", "name"]
				}
				this.$model.post("/cms/table/get_all", data).then(res => {
					if (res.code == 200) {
						this.properties = res.data
					}
				})
			},
			get_data() {
				const loading = this.$loading({
					lock: true,
					text: '加载中',
					spinner: 'el-icon-loading',
					background: 'rgba(0, 0, 0, 0.7)'
				}); //加载页放置显示页面之前
				this.$model.post("cms/table/get", this.search_form).then(res => {
					//（pipost中楼盘管理查询的地址，参数）.then
					if (res.code == 200) {
						loading.close();
						this.list = res.data.data
						this.total = res.data.total
					}
				})
			},
			handleClose() {
				this.visible = false
			
			},
			current_change(e) {
				this.search_form.page = e
				this.get_data()
			},
			edit_bt(row) {
				this.edit_type = 1
				this.form = row
				this.visible = true
			},
			submit() {
				this.form["table"] = "floors"
				this.$model.post(this.url[this.edit_type], this.form).then(res => {
					if (res.code == 200) {
						this.$message({
							message: this.edit_tips[this.edit_type],
							type: 'success'
						});
						this.clear()
						this.get_data()
					}
				})

			},
			clear() {
				this.edit_type = 0
				this.handleClose()
				this.form = {
					name: "",
					table: "properties",
					pid: ""
				}
			},
			add_bt() {
				this.visible = true
				this.edit_type = 0

			},
			del(id) {
				let url = "cms/table/del?&table=floors&id=" + id
				this.$model.put(url).then(res => {
					if (res.code == 200) {
						this.$message({
							message: '已删除',
							type: 'success'
						});
						this.search_form.page = 1
						this.get_data()
					}
				})
			}
		}
	}
</script>
<style>
</style>
